package com.personal.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	/**
	 * 将字符串拼接起来
	 * 
	 * @param arrayStr
	 * @return
	 */
	public static String joinString(String... arrayStr) {
		StringBuffer sb = new StringBuffer();
		for (String str : arrayStr) {
			sb.append(str);
		}
		return sb.toString();
	}

	public static String joinString(String delimiter, String[] arrayStr) {
		if (null == arrayStr || arrayStr.length == 0) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		int index = 0;
		int length = arrayStr.length;
		for (String str : arrayStr) {
			sb.append(str);
			if (++index != length) {
				sb.append(delimiter);
			}
		}
		return sb.toString();
	}

	public static Integer convertObjectToInteger(Object value) {
		if (null == value) {
			return 0;
		}

		String str = String.valueOf(value);
		if (str.trim().length() == 0) {
			return 0;
		}

		int result = 0;
		try {
			result = Integer.parseInt(str);
		} catch (NumberFormatException nfe) {
			return result;
		}
		return result;
	}

	public static Boolean convertObjectToBoolean(Object value) {
		if (null == value) {
			return null;
		}

		String str = String.valueOf(value);
		if (str.trim().length() == 0) {
			return null;
		}

		return Boolean.parseBoolean(str);
	}

	/**
	 * 将集合转换成Map，并将key值全部转换成小写。
	 * 
	 * @param set
	 * @return
	 */
	public static Map<String, String> convertMapToLower(Set<Entry<String, String>> set) {
		Map<String, String> map = new HashMap<String, String>();
		if (null == set || set.size() == 0) {
			return map;
		}

		for (Entry<String, String> entry : set) {
			map.put(entry.getKey().toLowerCase(), entry.getValue());
		}
		return map;
	}

	/**
	 * 不存在where,一般是有子查询的时候用到
	 * */
	public static String joinSelectSqlAddWhere(String selSql, String likeSql, String fieldSql, String orderSql) {
		StringBuffer sql = new StringBuffer();
		sql.append(selSql);
		if (null != likeSql && likeSql.trim().length() > 0 && null != fieldSql && fieldSql.trim().length() > 0) {
			sql.append(" where ");
			sql.append(likeSql);
			sql.append(" and ");
			sql.append(fieldSql);
		} else if (null != fieldSql && fieldSql.trim().length() > 0) {
			sql.append(" where ");
			sql.append(fieldSql);
		} else if (null != likeSql && likeSql.trim().length() > 0) {
			sql.append(" where ");
			sql.append(likeSql);
		}

		if (null != orderSql && orderSql.trim().length() > 0) {
			sql.append(orderSql);
		}
		return sql.toString();
	}

	/**
	 * 不管有没得where都可以调用，除了子查询
	 * */
	public static String joinSelectSql(String selSql, String likeSql, String fieldSql, String orderSql) {
		StringBuffer sql = new StringBuffer();
		sql.append(selSql);
		if (null != likeSql && likeSql.trim().length() > 0 && null != fieldSql && fieldSql.trim().length() > 0) {
			if (selSql.indexOf("where") != -1) {
				sql.append(" and ");
			} else {
				sql.append(" where ");
			}
			sql.append(likeSql);
			sql.append(" and ");
			sql.append(fieldSql);
		} else if (null != fieldSql && fieldSql.trim().length() > 0) {
			if (selSql.indexOf("where") != -1) {
				sql.append(" and ");
			} else {
				sql.append(" where ");
			}
			sql.append(fieldSql);
		} else if (null != likeSql && likeSql.trim().length() > 0) {
			if (selSql.indexOf("where") != -1) {
				sql.append(" and ");
			} else {
				sql.append(" where ");
			}

			sql.append(likeSql);
		}

		if (null != orderSql && orderSql.trim().length() > 0) {
			sql.append(orderSql);
		}
		return sql.toString();
	}

	public static String joinSelectSqlForField(String selSql, String address, String status) {
		StringBuffer sql = new StringBuffer();
		sql.append(selSql);
		if (null != address && address.trim().length() > 0 && null != status && status.trim().length() > 0) {
			sql.append(address);
			sql.append(" and ");
			sql.append(status);
		} else if (null != address && address.trim().length() > 0) {
			sql.append(address);
		} else if (null != status && status.trim().length() > 0) {
			sql.append(" and ");
			sql.append(status);
		}

		return sql.toString();
	}

	/**
	 * 已经存在where直接加and,一般是有子查询的时候用到
	 * */
	public static String joinSelectSqlByWhere(String selSql, String likeSql, String fieldSql, String orderSql) {
		StringBuffer sql = new StringBuffer();
		sql.append(selSql);
		if (null != likeSql && likeSql.trim().length() > 0 && null != fieldSql && fieldSql.trim().length() > 0) {
			sql.append(" and ");
			sql.append(likeSql);
			sql.append(" and ");
			sql.append(fieldSql);
		} else if (null != fieldSql && fieldSql.trim().length() > 0) {
			sql.append(" and ");
			sql.append(fieldSql);
		} else if (null != likeSql && likeSql.trim().length() > 0) {
			sql.append(" and ");
			sql.append(likeSql);
		}

		if (null != orderSql && orderSql.trim().length() > 0) {
			sql.append(orderSql);
		}
		return sql.toString();
	}

	public static String joinSelectSqlConditions(String selSql, String likeSql, String fieldSql, String orderSql) {
		StringBuffer sql = new StringBuffer();
		sql.append(selSql);
		sql.append(" where 1=1");
		if (!StringUtils.isEmpty(likeSql)) {
			sql.append(" and ");
			sql.append(fieldSql);
		}
		if (!StringUtils.isEmpty(fieldSql)) {
			sql.append(" and ");
			sql.append(fieldSql);
		}
		if (null != orderSql && orderSql.trim().length() > 0) {
			sql.append(orderSql);
		}
		return sql.toString();
	}

	public static String getSlug(String c) {
		String C = c.toUpperCase();
		String slug = "";
		try {
			Integer.parseInt(c);
			slug = c;
		} catch (Exception e) {
			if (c.equals(C)) {// 原来字母是大写，转为2位小写
				slug = C.toLowerCase() + C.toLowerCase();
			} else {
				slug = c;
			}
		}
		return slug;
	}



	/**
	 * 验证是否是手机号码
	 * 
	 * @author: ms
	 * @date: 2015年8月14日 下午1:54:04
	 * @version: v1.0
	 * @param mobiles
	 * @return
	 * @return: boolean
	 * @throws:
	 */
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public static String convertObjectToString(Object obj) {
		if (null == obj) {
			return "";
		}
		return obj.toString();
	}

	public static double convertObjectToDouble(Object obj) {
		if (null == obj) {
			return 0;
		}
		String str = String.valueOf(obj);
		if (str.trim().length() == 0) {
			return 0;
		}

		Double result = 0.0;
		try {
			result = Double.parseDouble(obj.toString());
		} catch (NumberFormatException nfe) {
			return result;
		}
		return result;
	}
	
}
